import { message } from 'ant-design-vue'

const CosAuth = require('~/assets/js/CosAuth')
// 获取临时密钥
let stsCache
const getCredentials = (initAuth) => {
    return new Promise((resolve, reject) => {
        if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
            return resolve(stsCache.credentials)
        }
        if (initAuth) {
            initAuth()
                .then((r) => {
                    stsCache = r
                    return resolve(stsCache.credentials)
                })
                .catch((err) => {
                    return reject(err)
                })
        }
    })
}

// 计算签名
const getAuthorization = (options) => {
    return new Promise((resolve, reject) => {
        getCredentials(options.initAuth)
            .then((credentials) => {
                return resolve({
                    XCosSecurityToken: credentials.sessionToken,
                    Authorization: CosAuth({
                        SecretId: credentials.tmpSecretId,
                        SecretKey: credentials.tmpSecretKey,
                        Method: options.Method,
                        Pathname: options.Pathname
                    })
                })
            })
            .catch((err) => {
                return reject(err)
            })
    })
}

export default ({ app }, inject) => {
    inject('service', {
        // cos鉴权
        getCreden (params) {
            return new Promise((resolve, reject) => {
                app.$ApiServer.cosAuth(params).then((response) => {
                    if (response.code !== 200) {
                        message.error(response.message)
                        reject(response.data)
                    }
                    resolve(response.data)
                })
            })
        },
        // 上传腾讯云
        getAuthor (options) {
            return getAuthorization({
                initAuth: this.getCreden,
                ...options
            })
        },
        // 转化时间格式
        dateFormatter (format) {
            const date = {
                'M+': new Date().getMonth() + 1,
                'd+': new Date().getDate(),
                'h+': new Date().getHours(),
                'm+': new Date().getMinutes(),
                's+': new Date().getSeconds(),
                'q+': Math.floor((new Date().getMonth() + 3) / 3),
                'S+': new Date().getMilliseconds()
            }
            if (/(y+)/i.test(format)) {
                format = format.replace(RegExp.$1, (new Date().getFullYear() + '').substr(4 - RegExp.$1.length))
            }
            if (/(S+)/i.test(format)) {
                format = format.replace(
                    RegExp.$1,
                    RegExp.$1.length === 1 ? date['S+'] : ('000' + date['S+']).substring(('' + date['S+']).length)
                )
            }
            for (const k in date) {
                if (new RegExp('(' + k + ')').test(format)) {
                    format = format.replace(
                        RegExp.$1,
                        RegExp.$1.length === 1 ? date[k] : ('00' + date[k]).substr(('' + date[k]).length)
                    )
                }
            }
            return format
        }
    })
}
